package br.pucrs.lognplay.faces.convert;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.pucrs.lognplay.entity.GenericEntity;

@FacesConverter(value = "genericEntityConverter")
public class GenericEntityConverter implements Converter {

	public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
		if (value == null || value.equals("")) {
			return "";
		} else {
			GenericEntity<?> entity = (GenericEntity<?>) value;
			String entityId = String.valueOf(entity.getId());
			component.getAttributes().put(entityId, entity);
			return entityId;
		}
	}

	public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
		if (value.trim().equals("")) {
			return null;
		} else {
			return component.getAttributes().get(value);
		}
	}
}
